package com.xyw.code.controller;


import com.xyw.code.constant.CommonStatusEnum;
import com.xyw.code.dto.RoleDTO;
import com.xyw.code.entity.SysRole;
import com.xyw.code.log.SysLog;
import com.xyw.code.service.ISysRoleService;
import com.xyw.code.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author xuyiwei
 * @since 2019-07-30
 */
@RestController
@RequestMapping("/role")
public class SysRoleController {

    @Resource
    private ISysRoleService roleService;

    /**
     * 获取角色列表
     *
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:role:view')")
    public Result getRoleList(@RequestParam(required = false) String roleName,@RequestParam(required = false) Integer deptId) {
        return Result.ok(roleService.selectRoleList(roleName,deptId));
    }

    /**
     * 保存角色及权限
     *
     * @return
     */
    @SysLog(descrption = "新增角色")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:role:add')")
    public Result add(@RequestBody RoleDTO roleDTO) {
        return Result.ok(roleService.saveRoleMenu(roleDTO));
    }

    /**
     * 更新角色
     *
     * @return
     */
    @SysLog(descrption = "更新角色")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:role:update')")
    public Result update(@RequestBody RoleDTO roleDTO) {
        return Result.ok(roleService.updateRoleMenu(roleDTO));
    }

    /**
     * 删除角色
     *
     * @return
     */
    @SysLog(descrption = "删除角色")
    @PostMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public Result delete(@PathVariable Integer id) {
        SysRole role = new SysRole();
        role.setId(id);
        role.setStatus(CommonStatusEnum.DELETE.getType());
        return Result.ok(roleService.updateById(role));
    }


    /**
     * 获取部门下拉框
     * @return
     */
    @PostMapping("/getDsTypes/{deptId}")
    public Result getDsTypes(@PathVariable Integer deptId) {
        return Result.ok(roleService.getDsTypes(deptId));
    }

    /**
     * 根据角色id获取菜单
     *
     * @param roleId
     * @return
     */
    @GetMapping("/findRoleMenus/{roleId}")
    public Result findRoleMenus(@PathVariable("roleId") Integer roleId) {
        return Result.ok(roleService.findMenuListByRoleId(roleId));
    }

}

